package com.bugsnag.ohos;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * ErrorInternal
 *
 * @since 2021-07-02
 */
public class ErrorInternal implements JsonStream.Streamable {
    String errorClass;
    String errorMessage;
    ErrorType type;

    List<Stackframe> stacktrace;

    /**
     * constructor
     *
     * @param errorClass
     * @param errorMessage
     * @param stacktrace
     */
    public ErrorInternal(String errorClass, String errorMessage, Stacktrace stacktrace) {
        this(errorClass, errorMessage, stacktrace, ErrorType.OHOS);
    }

    /**
     * constructor
     *
     * @param errorClass
     * @param errorMessage
     * @param stacktrace
     * @param type
     */
    public ErrorInternal(String errorClass, String errorMessage, Stacktrace stacktrace, ErrorType type) {
        this.errorClass = errorClass;
        this.errorMessage = errorMessage;
        this.type = type;
        this.stacktrace = stacktrace.getTrace();
    }

    public String getErrorClass() {
        return errorClass;
    }

    public void setErrorClass(String errorClass) {
        this.errorClass = errorClass;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public ErrorType getType() {
        return type;
    }

    public void setType(ErrorType type) {
        this.type = type;
    }

    public List<Stackframe> getStacktrace() {
        return stacktrace;
    }

    public void setStacktraces(List<Stackframe> stacktraces) {
        this.stacktrace = stacktraces;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        writer.name("errorClass").value(errorClass);
        writer.name("message").value(errorMessage);
        writer.name("type").value(type.desc);
        writer.name("stacktrace").value(stacktrace);
        writer.endObject();
    }

    /**
     * create error list
     *
     * @param exc
     * @param projectPackages
     * @param logger
     * @return errors
     */
    public static List<Error> createError(Throwable exc, Collection<String> projectPackages, Logger logger) {
        List<Throwable> list = ThrowableUtils.safeUnrollCauses(exc);
        List<Error> errors = new ArrayList<>();
        for (Throwable currentEx : list) {
            StackTraceElement[] stacktrace = currentEx.getStackTrace();
            if (stacktrace == null) {
                stacktrace = new StackTraceElement[]{};
            }
            Stacktrace trace = new Stacktrace(stacktrace, projectPackages, logger);
            ErrorInternal errorInternal =
                new ErrorInternal(currentEx.getClass().getName(), currentEx.getLocalizedMessage(), trace);
            Error error = new Error(errorInternal, logger);
            errors.add(error);
        }
        return errors;
    }
}
